package Week.Week316;

import java.util.LinkedList;

public class p2 {
    class Solution {
        public int subarrayGCD(int[] nums, int k) {
            if (nums.length < 2) return 0;
            LinkedList<int[]> list = new LinkedList<>();
            int index = 0;
            int slow = 0;
            for (int i = 0; i < nums.length; i++){
                index++;
                if (nums[i] < k){
                    int[] arr = new int[index];
                    System.arraycopy(nums, slow, arr, 0, index);
                    slow = i + 1;
                    if (index >= 2){
                        list.add(arr);
                    }
                    index = 0;
                }
            }
            int ans = 0;
            for (int[] arr : list) {
                int c;
                for (c = 0; c < arr.length; c++) {
                    if (arr[c] == k) {
                        ans++;
                        break;
                    }
                }
                if (c > 0 && arr[c - 1] % k == 0) {
                    ans++;
                } else if (c + 1 < arr.length && arr[c + 1] % k == 0) {
                    ans++;
                }
            }
            return ans;
        }
    }
}
